<?php
/**
 *
 * ModTask class file
 *
 * @category   UC
 * @author     刘海涛 <liuhaitao01@snda.com>
 * @copyright  1997-2010 SDO Group
 * @version    Release: 1.0.0
 * @link       http://www.sdo.com
 * @license    http://www.sdo.com
 */
defined('DB_DEV_SERVER') || define('DB_DEV_SERVER',false);

require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
require_once(dirname(__FILE__).'/../../config/UCConfTask.php');

class UCModTask
{
	private $_user_id	= '';
	
	/**
	 * 构造函数
	 *
	 */
	function __construct($user_id)
	{
		$this->_user_id = $user_id;
	}
	
	/**
	 * 析构函数
	 *
	 */
	function __destruct()
	{
		$this->_user_id = '';
	}
	
	/**
	 * 添加用户任务流水
	 * @param	string	$ar_param	array(	'user_id'			=> '用户ID',
											'task_id'			=> '任务ID',
											'task_status'		=> '任务状态',
											'task_award_type'	=> '任务奖品类型',
											'task_award'		=> '任务奖励',
										)
	 * @return	int					正常返回流水ID 错误返回错误ID（<0）
	 *
	 */
	function addTask($ar_param)
	{
		if(!is_array($ar_param) || count($ar_param)<1)
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$ar_param['user_id'] = $ar_param['user_id'] ? $ar_param['user_id'] : $this->_user_id;
		if(empty($ar_param['user_id']))
		{
			return UCConfTask::TASK_ERROR_USER_EMPTY;
		}

		$ar_param['create_time'] = date('Y-m-d H:i:s');
		$ar_param['modify_time'] = date('Y-m-d H:i:s');
		foreach($ar_param as $key=>$value)
		{
			$s_key		.= ',' . $key;
			$s_value	.= ',?';
			$ar_tmp[]	=  $value;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$ar_param['user_id'],'task2dbconfig');
		$s_sql	= 'INSERT INTO ' . $o_pdo->database.'.' . $o_pdo->table. ' (' . trim($s_key,',') . ') VALUES (' . trim($s_value,',') . ')';
		if(!$o_pdo->exec_with_prepare($s_sql,$ar_tmp))
		{
			return UCConfTask::TASK_ERROR_DB ;
		}
		return $o_pdo->lastInsertId();
	}

	/**
	 * 获取用户可循环任务信息
	 * @param	string	$sql		查询语句
	 * @param	string	$user_id	用户ID
	 * @return	array				任务详细信息
	 *
	 */
	function getTask($s_where,$ar_param,$user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(empty($user_id) || empty($s_where) || empty($ar_param))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database. '.' . $o_pdo->table. ' WHERE '.$s_where;
		$query	= $o_pdo->query_and_prepare($s_sql,$ar_param);
		return $query->fetchAll(PDO::FETCH_ASSOC);
	}

	/**
	 * 获取用户可循环任务信息
	 * @param	int		$task_id	任务ID
	 * @param	string	$user_id	用户ID
	 * @return	array				任务详细信息
	 *
	 */
	function getCircleTask($task_id,$user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(empty($user_id) || empty($task_id))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database. '.' . $o_pdo->table. ' WHERE user_id=? AND task_id=? AND task_status>=? AND task_status<?';
		$query	= $o_pdo->query_and_prepare($s_sql,array($user_id,$task_id,UCConfTask::TASK_USER_STATUS_GET,UCConfTask::TASK_USER_STATUS_AWARD));
		return $query->fetchAll(PDO::FETCH_ASSOC);
	}

	/**
	 * 获取用户单个任务信息通过用户ID和任务ID
	 * @param	int		$task_id	任务ID
	 * @param	string	$user_id	用户ID
	 * @return	array				任务详细信息
	 *
	 */
	function getOneTaskByTaskid($task_id,$user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(empty($user_id) || empty($task_id))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database . '.' . $o_pdo->table. ' WHERE user_id=? AND task_id=?';
		$query	= $o_pdo->query_and_prepare($s_sql,array($user_id,$task_id));
		return $query->fetch(PDO::FETCH_ASSOC);
	}

	/**
	 * 获取用户单个任务信息通过流水ID
	 * @param	int		$id			任务流水号
	 * @param	string	$user_id	用户ID
	 * @return	array				任务详细信息
	 *
	 */
	function getOneTaskById($id,$user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(empty($id) || empty($user_id))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database . '.' . $o_pdo->table. ' WHERE ut_id=?';
		$query	= $o_pdo->query_and_prepare($s_sql,array($id));
		return $query->fetch(PDO::FETCH_ASSOC);
	}

	/**
	 * 获取用户任务列表信息
	 * @param	string	$user_id	入库参数数组
	 * @return	array				任务列表信息
	 *
	 */
	function getAllTask($user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(empty($user_id))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database . '.' . $o_pdo->table. ' WHERE user_id=?';
		$query	= $o_pdo->query_and_prepare($s_sql,array($user_id));
		return $query->fetchAll(PDO::FETCH_ASSOC);
	}

	/**
	 * 修改用户任务信息
	 * @param	int		$id			任务流水ID
	 * @param	array	$ar_param	array(	'task_status'		=> '任务状态',
											'task_award_type'	=> '任务奖品类型',
											'task_award'		=> '任务奖励',
											'create_time'		=> '任务获得时间',
										  )
	 * @param	string	$user_id	入库参数数组
	 * @return	int					正常返回1 错误返回错误ID（<0）
	 *
	 */
	function updateTaskInfo($id,$ar_param,$user_id='')
	{
		$user_id = $user_id ? $user_id : $this->_user_id;
		if(!is_array($ar_param) || count($ar_param)<1 || empty($id) || empty($user_id))
		{
			return UCConfTask::TASK_ERROR_PARAM;
		}

		foreach($ar_param as $key=>$value)
		{
			$s_update	.= ',' . $key . '=?';
			$ar_tmp[]	= $value;
		}
		$o_pdo	= PDOFactory::getPDO(UCConfTask::TASK_DB_FLAG,$user_id,'task2dbconfig');
		$s_sql	= 'UPDATE ' . $o_pdo->database . '.' . $o_pdo->table. ' SET '. trim($s_update,',') .' WHERE ut_id=?';
		$ar_tmp[] = $id;
		if(!$o_pdo->exec_with_prepare($s_sql,$ar_tmp))
		{
			return UCConfTask::TASK_ERROR_DB ;
		}
		return 1;
	}
}
?>